package com.cicadamob.contact.api.http.handler;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import android.os.Environment;

import com.cicadamob.contact.api.Utils.ContactConfig;
import com.cicadamob.contact.api.Utils.Logger.AppLogger;
import com.cicadamob.contact.api.http.bean.AssetCardDetail;
import com.cicadamob.contact.api.http.bean.AssetCardEdit;
import com.cicadamob.contact.api.http.dao.PersonPostsRequest;
import com.cicadamob.contact.api.http.parse2bean.AssetXml2bean;
import com.gmccgz.im.sdk.http.bean.MaxPostIdAndTypeId;
import com.gmccgz.im.sdk.http.bean.MediaUpDetail;
import com.gmccgz.im.sdk.http.bean.ParaIntStr;
import com.gmccgz.im.sdk.http.bean.TypeAndId;
import com.gmccgz.im.sdk.http.handler.MediaDownload;
import com.gmccgz.im.sdk.http.handler.MediaUploadStorage;
import com.gmccgz.im.sdk.http.handler.TransferCallBack;


public class PersonPostsHandle {

	/**
	 * 请求的数据
	 */
	public static String requestStr;

	// public static String requestStr2;

	public static String getRequestStr() {
		return requestStr;
	}

	public static void setRequestStr(String requestStr) {
		PersonPostsHandle.requestStr = requestStr;
	}

	/** 发布帖子 */
	public static String publishPost(String ipPort, String fromid, String imsi,
			String imei, String token, String messageId, String id, int type,
			String title, String text, ArrayList<MediaUpDetail> listMeida,
			int postType, String name, String address, String X, String Y) {
		
		String str = "http://%1$s/portal/api/publishPost";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.publishPost2(fromid, imsi, imei, token,
				messageId, id, type, title, text, listMeida, postType, name,
				address, X, Y);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);

		return reponse;
	}

	/** 发布帖子 */
	public static String test(String url) {
		String request = "";
		// String url =
		// "http://183.232.65.102:8181/portal/api/download?signature=fromid:15626261844,imsi:460016252326206,imei:356458051176857,token:A99M999A14CAAA2Q01202145637&type=&officeNumber=15626261844&timestamp=1414314499161&mediaId=ff80808149650ff4014965309a9a0000";
		String reponse = sendPostRequest(url, request);
		return reponse;
	}

	/**
	 * 获取帖子列表
	 * 
	 * 
	 * @param postType获取帖子的类型
	 *            ，如果type=-1则代表取所有的帖子。 取值范围如下： 1：通知 2：产品推介 3：竞争情报。
	 * @param page
	 *            页数，获取第几页
	 * @param count
	 *            每页返回记录数
	 * 
	 * @return
	 */

	public static String getPostListWithSpecify(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			int postType, int count, int page, String id, int type,
			String userId, int opType) {
		String str = "http://%1$s/portal/api/getPostList";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getPostList(fromid, imsi, imei, token,
				messageId, postType, count, page, id, type, userId, opType);

		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	public static String getPostList(String ipPort, String fromid, String imsi,
			String imei, String token, String messageId, int postType,
			int count, int page, String id, int type) {
		String str = "http://%1$s/portal/api/getPostList";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getPostList(fromid, imsi, imei, token,
				messageId, postType, count, page, id, type);

		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 发布资产
	 * 
	 * @param location
	 *            资产位置，包括经纬度，格式为：address||X||Y
	 * 
	 * @param assetLoc
	 *            发布人位置，包括地址和经纬度，格式为：address||X||Y，默认获取用户当前位置
	 * */
	public static String publishAsset(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String id, int type, String title, String text,
			ArrayList<MediaUpDetail> listMeida, String contentType,
			String name, String location, String assetLoc,ParaIntStr pis) {
		String str = "http://%1$s/portal/api/publishAsset";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.publishAsset(fromid, imsi, imei, token,
				messageId, id, type, title, text, listMeida, contentType, name,
				location, assetLoc,pis);

		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 获取资产列表
	 * 
	 * 
	 * @param contentType获取内容的类型
	 *            ，如果取值为空则代表取所有的帖子。 取值范围如下： 1：通知 2：产品推介 3：竞争情报。
	 * @param location
	 *            用户当前位置，包括地址和经纬度，格式为：address||X||Y
	 * @param radius
	 *            指定获取的资产半径，如果不指定，则默认按资产发布时间倒叙获取资产
	 * 
	 * @return
	 */
	public static String getAssetListWithSpecify(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			int contentType, String location, int radius, int count, int page,
			String returnPara, String queryCond, String id, int type,
			String userId, int OPType) {
		String str = "http://%1$s/portal/api/getAssetList";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getAssetListWithSpecify(fromid, imsi,
				imei, token, messageId, contentType, location, radius, count,
				page, returnPara, queryCond, id, type, userId, OPType);

		// System.out.println("request=" + requestStr);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	public static String getAssetList(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			int contentType, String location, int radius, int count, int page,
			String returnPara, String queryCond, String id, int type) {
		String str = "http://%1$s/portal/api/getAssetList";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getAssetList(fromid, imsi, imei, token,
				messageId, contentType, location, radius, count, page,
				returnPara, queryCond, id, type);

		// System.out.println("request=" + requestStr);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 获取资产列表.简
	 * 
	 * 
	 * @param contentType获取内容的类型
	 *            ，如果取值为空则代表取所有的帖子。 取值范围如下： 1：通知 2：产品推介 3：竞争情报。
	 * @param location
	 *            用户当前位置，包括地址和经纬度，格式为：address||X||Y
	 * @param radius
	 *            指定获取的资产半径，如果不指定，则默认按资产发布时间倒叙获取资产
	 * 
	 * @return
	 */
	public static String getAssetListBrief(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			int contentType, String location, int radius, int number,String returnPara,String queryCond) {
		String str = "http://%1$s/portal/api/getAssetListBrief";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getAssetListBrief(fromid, imsi, imei,
				token, messageId, contentType, location, radius, number,returnPara,queryCond);

		// System.out.println("request=" + requestStr);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 发表资产评论
	 * 
	 * @return
	 */
	public static String addAssetComment(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String postId, String name, String location, String text,
			ArrayList<MediaUpDetail> mediaIdList) {
		String str = "http://%1$s/portal/api/addAssetComment";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.addAssetComment(fromid, imsi, imei,
				token, messageId, postId, name, location, text, mediaIdList);

		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 获取资产详情
	 * 
	 * 
	 * @param postId
	 *            帖子id
	 * @param page
	 *            页数，获取第几页
	 * @param count
	 *            每页返回记录数
	 * 
	 * @return
	 */

	public static String getAssetDetail(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String location, String postId, int count, int page,String returnPara,
			String queryCond) {
		String str = "http://%1$s/portal/api/getAssetDetail";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getAssetDetail(fromid, imsi, imei,
				token, messageId, location, postId, count, page,returnPara,queryCond);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);

		return reponse;
	}

	/**
	 * 获取资产评论
	 * 
	 * 
	 * @param postId
	 *            帖子id
	 * @param page
	 *            页数，获取第几页
	 * @param count
	 *            每页返回记录数
	 * 
	 * @return
	 */

	public static String getAssetComment(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String postId, int count, int page) {
		String str = "http://%1$s/portal/api/getAssetComment";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getAssetComment(fromid, imsi, imei,
				token, messageId, postId, count, page);

		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 发表帖子评论
	 * 
	 * @return
	 */
	public static String addComment(String ipPort, String fromid, String imsi,
			String imei, String token, String messageId, String postId,
			String name, String address, String X, String Y, String text,
			ArrayList<MediaUpDetail> mediaIdList) {
		String str = "http://%1$s/portal/api/addComment";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.addComment(fromid, imsi, imei, token,
				messageId, postId, name, address, X, Y, text, mediaIdList);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 获取帖子详情
	 * 
	 * 
	 * @param postId
	 *            帖子id
	 * @param page
	 *            页数，获取第几页
	 * @param count
	 *            每页返回记录数
	 * 
	 * @return
	 */

	public static String getPostDetail(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String postId, int count, int page) {
		String str = "http://%1$s/portal/api/getPostDetail";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getPostDetail(fromid, imsi, imei,
				token, messageId, postId, count, page);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 获取帖子评论
	 * 
	 * 
	 * @param postId
	 *            帖子id
	 * @param page
	 *            页数，获取第几页
	 * @param count
	 *            每页返回记录数
	 * 
	 * @return
	 */

	public static String getComment(String ipPort, String fromid, String imsi,
			String imei, String token, String messageId, String postId,
			int count, int page) {
		String str = "http://%1$s/portal/api/getComment";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getPostDetail(fromid, imsi, imei,
				token, messageId, postId, count, page);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 删帖
	 * 
	 * 
	 * @param postId
	 *            帖子id
	 * 
	 * @return
	 */

	public static String deletePost(String ipPort, String fromid, String imsi,
			String imei, String token, String messageId, String postId) {
		String str = "http://%1$s/portal/api/deletePost";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.deletePost(fromid, imsi, imei, token,
				messageId, postId);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 删帖子评论
	 * 
	 * 
	 * @param postId
	 *            帖子id
	 * 
	 * @return
	 */

	public static String deletePostComment(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String commentId) {
		String str = "http://%1$s/portal/api/deletePostComment";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.deletePostComment(fromid, imsi, imei,
				token, messageId, commentId);

		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 删除帖子点赞
	 */
	public static String deletePostPraise(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String postId) {
		String str = "http://%1$s/portal/api/deletePostPraise";
		String url = String.format(str, ipPort);

		String request = PersonPostsRequest.deletePostPraiseRequest(fromid,
				imsi, imei, token, messageId, postId);

		String reponse = sendPostRequest(url, request);
		setRequestStr(url + "\n" + request);
		return reponse;

	}

	/**
	 * 帖子点赞
	 */
	public static String addPostPraise(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String postId, String name) {
		String str = "http://%1$s/portal/api/addPostPraise";
		String url = String.format(str, ipPort);

		String request = PersonPostsRequest.addPostPraiseRequest(fromid, imsi,
				imei, token, messageId, postId, name);

		String reponse = sendPostRequest(url, request);
		setRequestStr(url + "\n" + request);
		return reponse;

	}

	/**
	 * 资产点赞
	 */
	public static String addAssetPraise(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String assetId, String name) {
		String str = "http://%1$s/portal/api/addAssetPraise";
		String url = String.format(str, ipPort);

		String request = PersonPostsRequest.addAssetPraise(fromid, imsi, imei,
				token, messageId, assetId, name);

		String reponse = sendPostRequest(url, request);
		setRequestStr(url + "\n" + request);
		return reponse;

	}

	/**
	 * 新增内容分类
	 * 
	 * @param id
	 *            ：跟类型相对应，可为群组号或者企业编号;
	 * @param type
	 *            ： 1：群组 2：企业;
	 */

	public static String addContentType(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String id, int type, int classify, String mediaId, String name,
			String description, String creator) {
		String str = "http://%1$s/portal/api/addContentType";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.addContentType(fromid, imsi, imei,
				token, messageId, id, type, classify, mediaId, name,
				description, creator);

		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 修改内容分类
	 * 
	 * @param type
	 *            ：待修改的类型id
	 */

	public static String modifyContentType(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId, int type,
			String mediaId, String name, String description) {
		String str = "http://%1$s/portal/api/modifyContentType";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.modifyContentType(fromid, imsi, imei,
				token, messageId, type, mediaId, name, description);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 删除内容分类
	 */

	public static String deleteContentType(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId, int type) {
		String str = "http://%1$s/portal/api/deleteContentType";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.deleteContentType(fromid, imsi, imei,
				token, messageId, type);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 查询内容分类
	 * 
	 * @param id
	 *            跟类型相对应，可为群组号或者企业编号 type： 1：群组 2：企业
	 * @param postType
	 *            帖子类型 ，如果取值为空（-1）则代表取所有的帖子。 取值范围如下： 1：通知 2：产品推介 3：竞争情报。
	 */
	public static String queryContentType(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			String id, int type, int postType, int classify) {
		String str = "http://%1$s/portal/api/queryContentType";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.queryContentType(fromid, imsi, imei,
				token, messageId, id, type, postType, classify);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;
	}

	/**
	 * 编辑资产
	 */
	public static String editAsset(String ipPort, String fromid, String imsi,
			String imei, String token, String messageId, String id, int type,
			String title, String postId, String text,
			ArrayList<MediaUpDetail> listMeida, String contentType,
			String otherJsonStr, String location, String assetLoc,
			String homepage,ParaIntStr pis) {

		String str = "http://%1$s/portal/api/editAsset";
		String url = String.format(str, ipPort);
		requestStr = PersonPostsRequest.editAsset(fromid, imsi, imei, token,
				messageId, id, type, title, postId, text, listMeida,
				contentType, otherJsonStr, location, assetLoc, homepage,pis);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;

	}
	
	public static AssetCardDetail editAssetBean(AssetCardEdit ace) {
		String str = "http://%1$s/portal/api/editAsset";
		String url = String.format(str, ace.getIpPort());
		requestStr = PersonPostsRequest.editAssetBean(ace);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		
		String folderDir = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + "/ManagerAssistant/locationCard_Log";
		String fileName = "ContentCard";
		
		AppLogger.writeLogToFile(ContactConfig.getContext(), folderDir,
				fileName, "getCardDetail_xml=" + url + "\n" + requestStr, true);
		
		AssetCardDetail acd = AssetXml2bean.getAssetCardDetail(reponse);
		return acd;

	}

	/**
	 * 编辑内容分类
	 */
	public static String editContentType(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			int cardType, String title, String text,
			ArrayList<MediaUpDetail> listMeida, String homepage,
			String otherJsonStr, int classify, String id, int type,
			String location, ParaIntStr pis) {
		String str = "http://%1$s/portal/api/editContentType";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.editContentType(fromid, imsi, imei,
				token, messageId, cardType, title, text, listMeida, homepage,
				otherJsonStr, classify, id, type, location,pis);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);

		return reponse;

	}

	/**
	 * 设置内容分类权限
	 */
	public static String setContentTypeRight(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId, int type,
			ArrayList<TypeAndId> listAdmin, ArrayList<TypeAndId> listWriting,
			ArrayList<TypeAndId> listReading) {
		String str = "http://%1$s/portal/api/setContentTypeRight";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.setContentTypeRight(fromid, imsi, imei,
				token, messageId, type, listAdmin, listWriting, listReading);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);

		return reponse;

	}

	/**
	 * 设置内容分类权限admin
	 */
	public static String setContentTypeRightAdmin(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId, int type,
			ArrayList<TypeAndId> listAdmin) {
		String str = "http://%1$s/portal/api/setContentTypeRight";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.setContentTypeRightAdmin(fromid, imsi,
				imei, token, messageId, type, listAdmin);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);

		return reponse;

	}

	/**
	 * 设置内容分类权限read
	 */
	public static String setContentTypeRightReading(String ipPort,
			String fromid, String imsi, String imei, String token,
			String messageId, int type, ArrayList<TypeAndId> listReading) {
		String str = "http://%1$s/portal/api/setContentTypeRight";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.setContentTypeRightReading(fromid,
				imsi, imei, token, messageId, type, listReading);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);

		return reponse;

	}

	/**
	 * 4.8 获取分类帖子更新状态
	 */
	public static String getContentTypeUpdate(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId,
			ArrayList<MaxPostIdAndTypeId> listId) {
		String str = "http://%1$s/portal/api/getContentTypeUpdate";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getContentTypeUpdate(fromid, imsi,
				imei, token, messageId, listId);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);

		return reponse;

	}

	/**
	 * 设置内容分类权限write
	 */
	public static String setContentTypeRightWriting(String ipPort,
			String fromid, String imsi, String imei, String token,
			String messageId, int type, ArrayList<TypeAndId> listWriting) {
		String str = "http://%1$s/portal/api/setContentTypeRight";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.setContentTypeRightWriting(fromid,
				imsi, imei, token, messageId, type, listWriting);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);

		return reponse;

	}

	/**
	 * 获取资产名片
	 */
	public static String getAssetCard(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId, int type,
			String id, String postId, int version) {
		String str = "http://%1$s/portal/api/getAssetCard";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getAssetCard(fromid, imsi, imei, token,
				messageId, type, id, postId, version);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;

	}

	/**
	 * 获取资产名片详情
	 */
	public static String getAssetCardDetail(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId, int type,
			String id, String postId, int version, String location,String returnPara,
			String queryCond) {
		String str = "http://%1$s/portal/api/getAssetCardDetail";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getAssetCardDetail(fromid, imsi, imei,
				token, messageId, type, id, postId, version, location,returnPara,queryCond);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;

	}

	/**
	 * 获取分类名片 type：类型，包括个人（0），群组（1），企业（2）， 帖子分类（3），资产分类（4），资产（5） id：编号，跟type相对应。
	 * 个人：手机号 群组：群组编号 企业：企业编号 帖子分类：帖子类型编号 资产分类：资产类型编号 资产：资产编号。
	 */
	public static String getContentTypeCard(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId, int type,
			String id, int classify, int version, int cardType) {
		String str = "http://%1$s/portal/api/getContentTypeCard";
		String url = String.format(str, ipPort);

		requestStr = PersonPostsRequest.getContentTypeCard(fromid, imsi, imei,
				token, messageId, type, id, classify, version, cardType);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;

	}

	/**
	 * 获取分类名片detail
	 */
	public static String getContentTypeCardDetail(String ipPort, String fromid,
			String imsi, String imei, String token, String messageId, int type,
			String id, int classify, int version, int editType, String location,String returnPara,String queryCond) {
		String str = "http://%1$s/portal/api/getContentTypeCardDetail";
		String url = String.format(str, ipPort);

		
		requestStr = PersonPostsRequest.getContentTypeCardDetail(fromid, imsi,
				imei, token, messageId, type, id, classify, version, editType,
				location, returnPara, queryCond);
		String reponse = sendPostRequest(url, requestStr);
		setRequestStr(url + "\n" + requestStr);
		return reponse;

	}

	/**
	 * 上传名片的背景图链接
	 * 
	 * @param url
	 *            http://ip:port/portal/api/uploadCard?signature=fromid:FROMID,
	 *            imsi:IMSI,imei:IMEI,token:TOKEN&toUser=type:1,id:15918611748&
	 *            timestamp=TIMESTAMP
	 * @return
	 */
	public static String getUploadCardPicUrlIp(String ipPort, String fromid,
			String imsi, String imei, String token, String type, String id,
			String timestamp) {
		String str = "http://%1$s/portal/api/uploadCard";
		String url = String.format(str, ipPort);

		StringBuilder sb = new StringBuilder();
		sb.append(url);
		sb.append("?signature=fromid:");
		sb.append(fromid);
		sb.append(", imsi:");
		sb.append(imsi);
		sb.append(", imei:");
		sb.append(imei);
		sb.append(", token:");
		sb.append(token);
		sb.append("&toUser=type:");
		sb.append(type);
		sb.append(",id:");
		sb.append(id);
		sb.append("&timestamp=");
		sb.append(timestamp);
		requestStr = sb.toString();
		setRequestStr(requestStr);
		return requestStr;

	}

	public static String getUploadCardPicUrl(String url, String fromid,
			String imsi, String imei, String token, String type, String id,
			String timestamp) {

		StringBuilder sb = new StringBuilder();
		sb.append(url);
		sb.append("?signature=fromid:");
		sb.append(fromid);
		sb.append(", imsi:");
		sb.append(imsi);
		sb.append(", imei:");
		sb.append(imei);
		sb.append(", token:");
		sb.append(token);
		sb.append("&toUser=type:");
		sb.append(type);
		sb.append(",id:");
		sb.append(id);
		sb.append("&timestamp=");
		sb.append(timestamp);
		requestStr = sb.toString();
		setRequestStr(requestStr);
		return requestStr;

	}

	/**
	 * 上传PropertiesConfig.PCENTER_UPLOADRESOURCEURL/portal/api/upload
	 * */
	public static String upload(String url, String fromid, String toid,
			String usertype, String token, String imsi, String imei,
			String mediadetail, File file, int fileType,
			TransferCallBack callBack) {
		MediaUploadStorage mu = new MediaUploadStorage(url, file, fileType,
				fromid, toid, usertype, token, imsi, imei, mediadetail);
		mu.setCallBack(callBack);
		setRequestStr(mu.getUploadUrl());
		String result = mu.uploadMedia();
		return result;
	}

	public static String uploadIp(String ipPort, String fromid, String toid,
			String usertype, String token, String imsi, String imei,
			String mediadetail, File file, int fileType,
			TransferCallBack callBack) {
		// "http://"
		// + PERSONCENTERIP + "/portal/api/upload";
		String str = "http://%1$s/portal/api/upload";
		String url = String.format(str, ipPort);
		MediaUploadStorage mu = new MediaUploadStorage(url, file, fileType,
				fromid, toid, usertype, token, imsi, imei, mediadetail);
		mu.setCallBack(callBack);
		setRequestStr(mu.getUploadUrl());
		String result = mu.uploadMedia();
		return result;
	}

	public static String uploadCardIp(String ipPort, String fromid,
			String toid, String usertype, String token, String imsi,
			String imei, String mediadetail, File file, int fileType,
			TransferCallBack callBack) {
		// "http://"
		// + PERSONCENTERIP + "/portal/api/upload";
		String str = "http://%1$s/portal/api/uploadCard";
		String url = String.format(str, ipPort);
		MediaUploadStorage mu = new MediaUploadStorage(url, file, fileType,
				fromid, toid, usertype, token, imsi, imei, mediadetail);
		mu.setCallBack(callBack);
		setRequestStr(mu.getUploadUrl());
		String result = mu.uploadMedia();
		return result;
	}

	/**
	 * public static final String PCENTER_DOWNLOADRESOURCEURL = "http://" +
	 * PERSONCENTERIP + "/portal/api/download";
	 * 
	 * */
	public static String downloadIp(String ipPort, String media_id,
			String fromid, String token, String fileSavePath, String imsi,
			String imei, int type, String officenumber,
			TransferCallBack callBack) {
		String str = "http://%1$s/portal/api/download";
		String url = String.format(str, ipPort);

		MediaDownload md = new MediaDownload(url, media_id, fromid, token,
				fileSavePath, imsi, imei, type, officenumber);
		md.setCallBack(callBack);
		setRequestStr(md.getDownloadUrl());
		String result = md.downloadMedia();
		return result;
	}

	public static String download(String url, String media_id, String fromid,
			String token, String fileSavePath, String imsi, String imei,
			int type, String officenumber, TransferCallBack callBack) {

		MediaDownload md = new MediaDownload(url, media_id, fromid, token,
				fileSavePath, imsi, imei, type, officenumber);
		md.setCallBack(callBack);
		setRequestStr(md.getDownloadUrl());
		String result = md.downloadMedia();
		return result;
	}

	/**
	 * 下载链接 PropertiesConfig.PCENTER_DOWNLOADRESOURCEURL
	 * 
	 * @param targetUrl
	 * @param request
	 * @return
	 */
	public static String getDownUrl(String url, String fromid, String imsi,
			String imei, String token, int type, String timestamp,
			String mediaId) {

		String str = url + "?signature=fromid:" + fromid + ",imsi:" + imsi
				+ ",imei:" + imei + ",token:" + token + "&type=" + type
				+ "&officeNumber=" + fromid + "&timestamp=" + timestamp
				+ "&mediaId=" + mediaId;
		return str;

	}

	/**
	 * 下载链接 PropertiesConfig.PCENTER_DOWNLOADRESOURCEURL
	 * 
	 * @param targetUrl
	 * @param request
	 * @return
	 */
	public static String getDownUrlIp(String ipPort, String fromid,
			String imsi, String imei, String token, int type, String timestamp,
			String mediaId) {
		String s = "http://%1$s/portal/api/download";
		String url = String.format(s, ipPort);

		String str = url + "?signature=fromid:" + fromid + ",imsi:" + imsi
				+ ",imei:" + imei + ",token:" + token + "&type=" + type
				+ "&officeNumber=" + fromid + "&timestamp=" + timestamp
				+ "&mediaId=" + mediaId;
		return str;

	}

	public static String sendPostRequest(String targetUrl, String request) {
		// System.out.println(targetUrl);
		// System.out.println(request);
		long startTime = System.currentTimeMillis();
		InputStream in = null;
		DataOutputStream ds = null;
		String log_response = null;
		try {
			URL url = new URL(targetUrl);// MalformedURLException
			HttpURLConnection con = (HttpURLConnection) url.openConnection();// IOException
			con.setRequestMethod("POST");// ProtocolException
			con.setRequestProperty("Connection", "Keep-Alive");
			con.setRequestProperty("Charset", "UTF-8");
			con.setConnectTimeout(5 * 1000);
			con.setReadTimeout(5 * 1000);
			con.setDoInput(true);
			con.setDoOutput(true);
			con.setUseCaches(false);
			// con.setRequestProperty("Accept-Encoding", "gzip");

			ds = new DataOutputStream(con.getOutputStream()); // IOException
			if (null != request && !"".equals(request)) {
				ds.write(request.getBytes());
			}

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					con.getInputStream(), "UTF-8"));
			StringBuilder sb = new StringBuilder();
			String lineStr = null;
			while (null != (lineStr = reader.readLine())) {// IOException
				sb.append(lineStr);
			}
			log_response = sb.toString();
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			log_response = e.toString();
		} finally {
			// HttpLogUtil.writeLog(targetUrl, request, log_response, startTime,
			// System.currentTimeMillis());
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != ds) {
				try {
					ds.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

}
